﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyChungCu_V.BussinessTỉer
{
    class ClientManagerService
    {
        SQLConnection sql_conn;
        public ClientManagerService()
        {
            try
            {
                sql_conn = new SQLConnection(null); // Lay ConnectionString trong DataConnection.xml, neu file khong ton tai thi tao moi va ghi lai ConnectionString
            }
            catch (ApplicationException ex)
            {
                throw ex;
            }
        }
        


      
        public void AddClient(string name, int age)
        {
            QLCCDataContext ctx = new QLCCDataContext();
            ctx.Connection.ConnectionString = sql_conn.ConnectionString;
            ctx.KhachHangs.InsertOnSubmit(new KhachHang
            {
                TenKH = name,
                TuoiKH = age,
                GioiTinh = true,
        
                DiaChi = "",
               NgheNghiep = "",
                SoDT = " ",
                 SoCMT= " ",
                
            });
            ctx.SubmitChanges();
        }
       
        
       
        public IList<KhachHangDTO> FindClient(string name)
        {
            QLCCDataContext ctx = new QLCCDataContext();
            ctx.Connection.ConnectionString = sql_conn.ConnectionString;

            var client = from p in ctx.KhachHangs
                         where p.TenKH.Contains(name)
                         select new KhachHangDTO
                         {
                             id = p.idKhachHang,
                             name = p.TenKH,
                             age = (int)p.TuoiKH,
                             id_card_no = p.SoCMT,
                             id_job = p.NgheNghiep,
                             address = p.DiaChi,
                             phone_num = p.SoDT,
                             sex = p.GioiTinh,
                             
                         };
            return client.ToList<KhachHangDTO>();
        }
    }
}
